package com.meiyu.live.ui.main;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import com.meiyu.live.BaseFragment;
import com.meiyu.live.R;
import com.meiyu.live.ui.columnfollow.ColumnFollowActivity;
import com.meiyu.live.ui.search.SearchActivity;
import com.meiyu.live.util.DisplayUtils;
import com.meiyu.live.widget.PagerSlidingTabStrip;

public class HomeFragment extends BaseFragment implements View.OnClickListener {

    private String[] tabName = {"正在热播", "轻松一刻", "我的订阅"};

    private PagerSlidingTabStrip tabStrip;
    private ViewPager viewPager;
    private HomePagerAdapter pagerAdapter;
    private View btn_add, btn_search;

    public HomeFragment() {
    }

    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void init() {
        btn_search = findViewById(R.id.btn_search);
        btn_search.setOnClickListener(this);
        btn_add = findViewById(R.id.btn_add);
        btn_add.setOnClickListener(this);

        tabStrip = (PagerSlidingTabStrip) findViewById(R.id.tab_strip);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        pagerAdapter = new HomePagerAdapter(getChildFragmentManager());
        viewPager.setAdapter(pagerAdapter);
        tabStrip.setViewPager(viewPager);
        tabStrip.setTextSize(DisplayUtils.dpToPixel(getActivity(), 16));
        viewPager.setCurrentItem(0);
    }

    @Override
    public int getContextView() {
        return R.layout.fragment_home;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onClick(View view) {
        if (view == btn_search) {
            Intent intent = new Intent(getActivity(), SearchActivity.class);
            getActivity().startActivity(intent);
        } else if (view == btn_add) {
            Intent intent = new Intent(getActivity(), ColumnFollowActivity.class);
            getActivity().startActivity(intent);
        }
    }

    class HomePagerAdapter extends FragmentPagerAdapter {

        public HomePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return tabName.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabName[position];
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        @Override
        public void startUpdate(ViewGroup container) {
            super.startUpdate(container);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = null;
            switch (position) {
                case 0: {
                    fragment = HotliveFragment.newInstance(1);
                }
                break;
                case 1: {
                    fragment = HotliveFragment.newInstance(0);
                }
                break;
                case 2: {
                    fragment = MySubscribeLiveFragment.newInstance();
                }
                break;
            }
            return fragment;
        }
    }

}
